package com.company.hotel.web;
import com.company.hotel.core.Result;
import com.company.hotel.core.ResultGenerator;
import com.company.hotel.model.pojo.User;
import com.company.hotel.service.UserService2;
import com.company.hotel.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* 采用restcontroller 返回的结果以rest风格返回数据格式为json
*/
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService usersService;
    @Resource
    private UserService2 userService2;

    @PostMapping("/add")
    public Result add(User users) {
        userService2.save((List<com.company.hotel.model.User>) users);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        userService2.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(com.company.hotel.model.User users) {
        userService2.update(users);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        com.company.hotel.model.User users = userService2.findById(id);
        return ResultGenerator.genSuccessResult(users);
    }

    @RequestMapping("/lists")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<com.company.hotel.model.User> list = userService2.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    @RequiresPermissions("query")
    @RequestMapping("/te")
    public String te(){
        return "sdfs";
    }
    @RequestMapping("/list")
    public Result list() {
        return ResultGenerator.genSuccessResult();
    }
    @RequiresPermissions("query")
    @RequestMapping("/id")
    public Result details(@RequestParam int id) {
        List<User> users = usersService.findByUserId(id);
        return ResultGenerator.genSuccessResult(users);
    }


}
